package com.riatimes.util
{

	/**
	 *
	 * 字符处理类
	 */

	public class StringUtil
	{

		/**
		 * 返回把秒数格式化分秒后的字符串
		 * @param t
		 * @return
		 */
		public static function formatMinitesTime(t:uint):String
		{
			var m:Number=Math.floor((t / 60));
			var s:Number=t % 60;
			var sm:String=m > 9 ? m.toString() : "0" + m.toString();
			var ss:String=s > 9 ? s.toString() : "0" + s.toString();
			return sm + ":" + ss;
		}

		/**
		 * 返回把秒数格式化小时分秒后的字符串
		 * @param t
		 * @return
		 */
		public static function formatShortTime(t:uint):String
		{
			var h:Number=Math.floor(t / 3600);
			var m:Number=Math.floor((t / 60)) % 60;
			var s:Number=t % 60;

			var sh:String=h > 9 ? h.toString() : "0" + h.toString();
			var sm:String=m > 9 ? m.toString() : "0" + m.toString();
			var ss:String=s > 9 ? s.toString() : "0" + s.toString();
			return sh + ":" + sm + ":" + ss;
		}

		/**
		 * 返回把当前时间格式化为年月日后的字符串
		 * @param datetime
		 * @param prex
		 * @return
		 */
		public static function formatDate(datetime:Number, prex:String="-"):String
		{
			var date:Date=new Date(datetime);
			var year:Number=date.getFullYear();
			var month:Number=date.getMonth() + 1;
			var day:Number=date.getDate();
			
			var hour:Number=date.getHours();
			var minutes:Number=date.getMinutes();
			
			return year + prex + (month > 9 ? month : "0" + month) + prex + day+" "+hour+":"+minutes;
		}

		


		/**
		 * 字符替换
		 * @param sth
		 * @param charToChange
		 * @param newChar
		 * @return
		 */
		public static function ReplaceChar(sth:String, charToChange:String, newChar:String):String
		{
			var tmp:Array=sth.split(charToChange);
			return tmp.join(newChar);
		}

		/**
		 * 转义HTML字符
		 * @param txt
		 * @return
		 */
		public static function AntiHTML_Parser(txt:String):String
		{
			txt=ReplaceChar(txt, "<", "&#60;");
			txt=ReplaceChar(txt, ">", "&#62;");
			txt=ReplaceChar(txt, "\n", "");
			txt=ReplaceChar(txt, "newline", "");
			return (txt);
		}
		;

		/**
		 * 给web字符加上连接URl
		 * @param msg
		 * @return
		 */
		public static function HiliteURLs(msg:String):String
		{
			if (msg != "")
			{
				var extList:Array=new Array("http:", "www.", "ftp:", "ftp.", "ed2k:", "mailto:", "https:", "ftps:", "irc:");
				var url_begin:Number=-1;
				for (var i:Number=0; i < extList.length && url_begin == -1; i++) //Stop if found
					url_begin=msg.indexOf(extList[i]);

				if (url_begin == -1)
					return msg;
				var hilited:String=msg.substring(0, url_begin);
				var url_end:Number=msg.indexOf(" ", url_begin);
				if (url_end == -1)
					url_end=msg.indexOf("&#60;", url_begin);
				if (url_end == -1)
					url_end=msg.indexOf("&#62;", url_begin);

				var urlstr:String="";
				if (url_end == -1)
					urlstr=msg.substring(url_begin);
				else
					urlstr=msg.substring(url_begin, url_end);

				var urlref:String=urlstr;

				if (urlstr.indexOf("www.") == 0)
					urlref="http://" + urlstr;
				else if (urlstr.indexOf("ftp.") == 0)
					urlref="ftp://" + urlstr;
				var trailer:String="";
				if (url_end != -1)
					trailer=HiliteURLs(msg.substring(url_end));
				hilited+="<font color=\"#0000FF\"><u><a href=\"" + urlref + "\" target=\"_blank\">" + urlstr + "</a></u></font>" + trailer;
				return hilited;
			}
			else
				return null;
		}

		/**
		*	Does a case insensitive compare or two strings and returns true if
		*	they are equal.
		*
		*	@param s1 The first string to compare.
		*
		*	@param s2 The second string to compare.
		*
		*	@returns A boolean value indicating whether the strings' values are
		*	equal in a case sensitive compare.
		*
		* 	@langversion ActionScript 3.0
		*	@playerversion Flash 9.0
		*	@tiptext
		*/
		public static function stringsAreEqual(s1:String, s2:String, caseSensitive:Boolean):Boolean
		{
			if (caseSensitive)
			{
				return (s1 == s2);
			}
			else
			{
				return (s1.toUpperCase() == s2.toUpperCase());
			}
		}

		/**
		*	Removes whitespace from the front and the end of the specified
		*	string.
		*
		*	@param input The String whose beginning and ending whitespace will
		*	will be removed.
		*
		*	@returns A String with whitespace removed from the begining and end
		*
		* 	@langversion ActionScript 3.0
		*	@playerversion Flash 9.0
		*	@tiptext
		*/
		public static function trim(input:String):String
		{
			return StringUtil.ltrim(StringUtil.rtrim(input));
		}

		/**
		*	Removes whitespace from the front of the specified string.
		*
		*	@param input The String whose beginning whitespace will will be removed.
		*
		*	@returns A String with whitespace removed from the begining
		*
		* 	@langversion ActionScript 3.0
		*	@playerversion Flash 9.0
		*	@tiptext
		*/
		public static function ltrim(input:String):String
		{
			var size:Number=input.length;
			for (var i:Number=0; i < size; i++)
			{
				if (input.charCodeAt(i) > 32)
				{
					return input.substring(i);
				}
			}
			return "";
		}

		/**
		*	Removes whitespace from the end of the specified string.
		*
		*	@param input The String whose ending whitespace will will be removed.
		*
		*	@returns A String with whitespace removed from the end
		*
		* 	@langversion ActionScript 3.0
		*	@playerversion Flash 9.0
		*	@tiptext
		*/
		public static function rtrim(input:String):String
		{
			var size:Number=input.length;
			for (var i:Number=size; i > 0; i--)
			{
				if (input.charCodeAt(i - 1) > 32)
				{
					return input.substring(0, i);
				}
			}

			return "";
		}

		/**
		*	Determines whether the specified string begins with the spcified prefix.
		*
		*	@param input The string that the prefix will be checked against.
		*
		*	@param prefix The prefix that will be tested against the string.
		*
		*	@returns True if the string starts with the prefix, false if it does not.
		*
		* 	@langversion ActionScript 3.0
		*	@playerversion Flash 9.0
		*	@tiptext
		*/
		public static function beginsWith(input:String, prefix:String):Boolean
		{
			return (prefix == input.substring(0, prefix.length));
		}

		/**
		*	Determines whether the specified string ends with the spcified suffix.
		*
		*	@param input The string that the suffic will be checked against.
		*
		*	@param prefix The suffic that will be tested against the string.
		*
		*	@returns True if the string ends with the suffix, false if it does not.
		*
		* 	@langversion ActionScript 3.0
		*	@playerversion Flash 9.0
		*	@tiptext
		*/
		public static function endsWith(input:String, suffix:String):Boolean
		{
			return (suffix == input.substring(input.length - suffix.length));
		}

		/**
		*	Removes all instances of the remove string in the input string.
		*
		*	@param input The string that will be checked for instances of remove
		*	string
		*
		*	@param remove The string that will be removed from the input string.
		*
		*	@returns A String with the remove string removed.
		*
		* 	@langversion ActionScript 3.0
		*	@playerversion Flash 9.0
		*	@tiptext
		*/
		public static function remove(input:String, remove:String):String
		{
			return StringUtil.replace(input, remove, "");
		}

		/**
		*	Replaces all instances of the replace string in the input string
		*	with the replaceWith string.
		*
		*	@param input The string that instances of replace string will be
		*	replaces with removeWith string.
		*
		*	@param replace The string that will be replaced by instances of
		*	the replaceWith string.
		*
		*	@param replaceWith The string that will replace instances of replace
		*	string.
		*
		*	@returns A new String with the replace string replaced with the
		*	replaceWith string.
		*
		* 	@langversion ActionScript 3.0
		*	@playerversion Flash 9.0
		*	@tiptext
		*/
		public static function replace(input:String, replace:String, replaceWith:String):String
		{
			//change to StringBuilder
			var sb:String=new String();
			var found:Boolean=false;

			var sLen:Number=input.length;
			var rLen:Number=replace.length;

			for (var i:Number=0; i < sLen; i++)
			{
				if (input.charAt(i) == replace.charAt(0))
				{
					found=true;
					for (var j:Number=0; j < rLen; j++)
					{
						if (!(input.charAt(i + j) == replace.charAt(j)))
						{
							found=false;
							break;
						}
					}

					if (found)
					{
						sb+=replaceWith;
						i=i + (rLen - 1);
						continue;
					}
				}
				sb+=input.charAt(i);
			}
			//TODO : if the string is not found, should we return the original
			//string?
			return sb;
		}

		/**
		*	Specifies whether the specified string is either non-null, or contains
		*  	characters (i.e. length is greater that 0)
		*
		*	@param s The string which is being checked for a value
		*
		* 	@langversion ActionScript 3.0
		*	@playerversion Flash 9.0
		*	@tiptext
		*/
		public static function stringHasValue(s:String):Boolean
		{
			//todo: this needs a unit test
			return (s != null && s.length > 0);
		}

		/**
		 *
		 * @param str
		 * @param rest
		 * @return
		 */
		public static function substitute(str:String, ... rest):String
		{
			// Replace all of the parameters in the msg string.
			var len:uint=rest.length;
			var args:Array;
			if (len == 1 && rest[0] is Array)
			{
				args=rest[0] as Array;
				len=args.length;
			}
			else
			{
				args=rest;
			}

			for (var i:int=0; i < len; i++)
			{
				str=str.replace(new RegExp("\\{" + i + "\\}", "g"), args[i]);
			}

			return str;
		}
	}
}
